package com.piz.dao.product;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.piz.dao.common.PizHiberImpl;
import com.piz.dao.common.UnexpectedDBException;
import com.piz.model.ItemClassification;

/**
 * @author prameetk.sharma
 *
 */
public class ProductClassificationDao extends PizHiberImpl<ItemClassification>{
	
	/**
	 * @param itemClass
	 * @return
	 */
	public Long addProductClass(ItemClassification itemClass)
	{
		try {
			saveOrUpdate(itemClass);
			return itemClass.getId();
			
		} catch (Exception e) {
			throw new UnexpectedDBException("error in adding product class");
		}
		
	}
	
	/**
	 * @param id
	 * @return
	 */
	public boolean deleteProductClass(Long id)
	{
		try {
			ItemClassification itemClass = search(ItemClassification.class, id);
			delete(itemClass);
			return true;
		} catch (Exception e) {
			throw new UnexpectedDBException("error in deleting product class",e);
		}
	}
	
	/**
	 * @return
	 */
	public List<ItemClassification> getAllItemCategories()
	{
		try {
			String hqlQuery = "from ItemClassification";
			return (List<ItemClassification>) executeHQL(hqlQuery);
		} catch (Exception e) {
			throw new UnexpectedDBException("DBException in fetching item categories.",e);
		}
		
	}
	
	/**
	 * @param className
	 * @return
	 */
	public List<ItemClassification> fetchClassByName(String className) {
		
		try {
			String hqlQuery = "from ItemClassification";
			if(className != null && className != "")
			{
				hqlQuery += " where name= '"+className+"'";
			}
			return (List<ItemClassification>) executeHQL(hqlQuery);
		} catch (Exception e) {
			throw new UnexpectedDBException("DBException in fetching item categories by name.",e);
		}
		
	}
	
	/**
	 * @param id
	 * @return
	 */
	public ItemClassification fetchClassById(Long id) {
		
		try {
			ItemClassification classs = search(ItemClassification.class, id);
			return classs;
		} catch (Exception e) {
			throw new UnexpectedDBException("DBException in fetching item categories by ID.",e);
		}
		
	}

}
